package cn.edu.neu.lab603.cloudeval.monitor

import cn.edu.neu.lab603.cloudeval.CloudEval
import cn.edu.neu.lab603.cloudeval.entity.{DataCenter, PhysicalMachine => Host}
import cn.edu.neu.lab603.des.SimMonitor

import scala.collection.mutable
import scala.collection.mutable.ListBuffer

/** 监控主机利用率的历史记录。
  *
  * Created by yfwz100 on 2016/11/9.
  */
class HostUtilizationMonitor extends SimMonitor[CloudEval] {

  private val utilHistory = new mutable.HashMap[Host, ListBuffer[Double]]

  override def isMatch(context: CloudEval): Boolean = context.getCurrentTask.isInstanceOf[DataCenter.UpdateEvent]

  override def before(context: CloudEval): Unit = {}

  override def after(context: CloudEval): Unit = {
    context.dataCenters.foreach { dc =>
      dc.pmList.foreach { host =>
        utilHistory.getOrElseUpdate(host, new ListBuffer[Double]) += host.getUsedResPct("mips")
      }
    }
  }

  /** 获取历史记录。 */
  def getHistory(host: Host) = utilHistory.get(host).map(_.toList).getOrElse(List.empty)
}
